﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using edowork.com.SQL2005;
using edowork.com.Model;

namespace WebInvoicingsys.system.baseset.productdata
{
    public partial class product_edit : System.Web.UI.Page
    {
        //前台输出提示信息变量
        public string alert_msg = "";

        //传入的参数
        string id = "",  productcode, productname, productshortname, goodsclass, goodsunit, goodsspec, stack, guard;

        /// <summary>
        /// 取得传入的参数
        /// </summary>
        /// <returns></returns>
        private bool GetParma()
        {
            try
            {
                id = Request.Params["id"].ToString();
                productcode = Request.Params["productcode"].ToString();
                productname = Request.Params["productname"].ToString();
                productshortname = Request.Params["productshortname"].ToString();
                goodsclass = Request.Params["goodsclass"].ToString();
                goodsunit = Request.Params["goodsunit"].ToString();
                goodsspec = Request.Params["goodsspec"].ToString();
                stack = Request.Params["stack"].ToString();
                guard = Request.Params["guard"].ToString();
            }
            catch
            {
                //显示出错信息
                alert_msg = String.Format(Scripts.alert_warning, Errormsg.er_Parampost);
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);
                return false;
            }

            return true;
        }

        private void bind()
        {
            Execute exec = new Execute(Session["connectionString"].ToString());

            string sql = "", error_msg = "";

            sql = @"select * from product where productcode = '{0}'";

            sql = String.Format(sql, id);

            DataSet ds = exec.ExecSQLQueryDataSet(sql, ref error_msg);

            if ((ds == null) || (ds.Tables.Count <= 0) || (ds.Tables[0].Rows.Count <= 0))
            {
                alert_msg = String.Format(Scripts.alert_warning, error_msg);
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);
                return;
            }

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                txt_productcode.Text = dr["productcode"].ToString();
                txt_productname.Text = dr["productname"].ToString();
                txt_productshortname.Text = dr["productshortname"].ToString();
                uc_from_goodsclass.SetByValue(dr["goodsclassid"].ToString());
                uc_from_goodsunit.SetByValue(dr["goodsunitid"].ToString());
                uc_from_goodsspec.SetByValue(dr["goodsspecid"].ToString());
                uc_from_stack.SetByValue(dr["stackid"].ToString());
                uc_from_guard.SetByValue(dr["guardid"].ToString());
                txt_maxqty.Text = dr["maxqty"].ToString();
                txt_minqty.Text = dr["minqty"].ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ;
            }
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetParma();

                bind();
            }
        }

        //保存
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (GetParma() == false)
            {
                return;
            }

            alert_msg = "";

            decimal maxqty = 0, minqty = 0;

            if (Decimal.TryParse(txt_maxqty.Text.Trim(), out maxqty) == false)
            {
                alert_msg = String.Format(Scripts.alert_warning, "请输入最高库存量或输入的最高库存量格式有误");

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                txt_maxqty.Focus();

                return;
            }

            if (Decimal.TryParse(txt_minqty.Text.Trim(), out minqty) == false)
            {
                alert_msg = String.Format(Scripts.alert_warning, "请输入最低库存量或输入的最低库存量格式有误");

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                txt_minqty.Focus();

                return;
            }


            edowork.com.Model.productInfo productInfo = new productInfo();

            productInfo.Productname = txt_productname.Text.Trim();
            productInfo.Productshortname = txt_productshortname.Text.Trim();
            productInfo.Goodsclassid = uc_from_goodsclass.getValue();
            productInfo.Goodsunitid = uc_from_goodsunit.getValue();
            productInfo.Goodsspecid = uc_from_goodsspec.getValue();
            productInfo.Stackid = uc_from_stack.getValue();
            productInfo.Guardid = uc_from_guard.getValue();
            productInfo.Maxqty = maxqty;
            productInfo.Minqty = minqty;

            if (String.IsNullOrEmpty(productInfo.Productname))
            {
                alert_msg = String.Format(Scripts.alert_warning, "请输入货品全称");

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                txt_productname.Focus();

                return;
            }

            if (String.IsNullOrEmpty(productInfo.Productshortname))
            {
                alert_msg = String.Format(Scripts.alert_warning, "请输入货品简称");

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                txt_productshortname.Focus();

                return;
            }

            if (String.IsNullOrEmpty(productInfo.Goodsclassid))
            {
                alert_msg = String.Format(Scripts.alert_warning, "请选择货品类别");

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                return;
            }

            if (String.IsNullOrEmpty(productInfo.Goodsunitid))
            {
                alert_msg = String.Format(Scripts.alert_warning, "请选择货品单位");

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                return;
            }

            if (String.IsNullOrEmpty(productInfo.Goodsspecid))
            {
                alert_msg = String.Format(Scripts.alert_warning, "请选择货品规格");

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                return;
            }

            if (String.IsNullOrEmpty(productInfo.Stackid))
            {
                alert_msg = String.Format(Scripts.alert_warning, "请选择堆放方式");

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                return;
            }

            if (String.IsNullOrEmpty(productInfo.Guardid))
            {
                alert_msg = String.Format(Scripts.alert_warning, "请选择防护方式");

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                return;
            }

            //保存数据入数据库
            using (edowork.com.SQL2005.TranUtil tranutil = new TranUtil(Session["connectionString"].ToString()))
            {
                System.Data.SqlClient.SqlTransaction tran = null;

                try
                {
                    tran = tranutil.GetSqlTransaction();
                }
                catch (Exception er)
                {
                    alert_msg = String.Format(Scripts.alert_warning, er.Message);

                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                    return;
                }

                edowork.com.SQL2005.Execute exec = new Execute(Session["connectionString"].ToString());

                string sql = "";
                int result = -1;

                //保存入product表
                sql = String.Format(@"update product set productname='{1}', productshortname='{2}', goodsclassid='{3}', goodsunitid='{4}', 
                                                         goodsspecid='{5}', stackid='{6}', guardid='{7}', maxqty='{8}', minqty='{9}'
                                         where productcode='{0}'",
                                    id,
                                    productInfo.Productname, productInfo.Productshortname,
                                    productInfo.Goodsclassid, productInfo.Goodsunitid, productInfo.Goodsspecid,
                                    productInfo.Stackid, productInfo.Guardid, productInfo.Maxqty, productInfo.Minqty);

                result = exec.ExecSQLQueryInt(sql, ref alert_msg, tran);

                if (result <= 0)
                {
                    tran.Rollback();
                    tranutil.CloseConnection();

                    alert_msg = String.Format(Scripts.alert_warning, alert_msg);

                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                    return;
                }

                tran.Commit();
                tranutil.CloseConnection();
            }

            alert_msg = String.Format(Scripts.alert_success, Errormsg.success_Save);

            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);
        }

        //返回
        protected void btnReturn_Click(object sender, EventArgs e)
        {
            GetParma();

            string url = "/system/baseset/productdata/product_list.aspx?productcode={0}&productname={1}&productshortname={2}&goodsclass={3}&goodsunit={4}&goodsspec={5}&stack={6}&guard={7}";

            url = String.Format(url, productcode, productname, productshortname, goodsclass, goodsunit, goodsspec, stack, guard);

            Response.Redirect(url);
        }
    }
}